package com.haoshuang.zhouzhoubang.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityRemoveMemberBinding
import com.haoshuang.zhouzhoubang.model.bean.TeamGroupBean
import com.haoshuang.zhouzhoubang.ui.adapter.RemoveMemberAdapter
import com.haoshuang.zhouzhoubang.ui.dialog.ConfirmDialog
import com.haoshuang.zhouzhoubang.ui.vm.RemoveMemberViewModel
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.vertical


/**
 *
 */
class RemoveMemberActivity : BaseVMActivity<RemoveMemberViewModel, ActivityRemoveMemberBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<RemoveMemberActivity>()
        }
    }

    private var group_id = 0

    private val data = arrayListOf<TeamGroupBean.Member>()
    private val adapter = RemoveMemberAdapter(data)

    override fun initView(savedInstanceState: Bundle?) {
        group_id = intent.getIntExtra("group_id", 0)

        binding.titleBar.title = "分组管理"
        binding.titleBar.leftImageView().click { finish() }
        binding.recyclerView.vertical()
            .adapter = adapter

        adapter.setOnItemClick { view, position , item->

            data[position].isChecked = !data[position].isChecked
            adapter.notifyDataSetChanged()
        }

        binding.tvRemove.clickWithTrigger {
            val list = data.filter { it.isChecked }
            if (list.isEmpty()){
                toast("请选择成员")
                return@clickWithTrigger
            }
            XPopup.Builder(this)
                .asCustom(ConfirmDialog(this, content = "确定要移出当前选择的队员吗?", cancelStr = "再考虑考虑", confirmStr = "确定移出"){
                    val ids = list.joinToString(","){it.uid.toString()}
                    setResult(200, Intent().apply { putExtra("ids", ids) })
                    finish()
                }).show()

        }
    }

    override fun initData() {
        vm.getmemberListWithGroupId("group_id" to group_id)
    }

    override fun startObserve() {
        vm.getmemberListWithGroupIdData.observeState(this){
            onLoading  {showLoading()}
            onError {code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                data.clear()
                data.addAll(it?: emptyList())
                adapter.notifyDataSetChanged()
            }
        }
    }

}